var gulp = require('gulp');
var babel = require('gulp-babel');
var uglify = require('gulp-uglify');
var connect = require('gulp-connect');
var gutil = require('gulp-util');
var files = [
    "./pages/**/*.html",
    "./css/*.css",
    "./css/**/*.css",
    "./js/**/*.js"
];
gulp.task('watch',function(){
    gulp.watch(files,['change','js']);
});
gulp.task('connect',function(){
    connect.server({
        root:'./',
        host:'192.168.3.109',
        livereload:true
    })
});
gulp.task('js',function () {
    return gulp.src('js/**/*.js')
    //将ES6代码转译为可执行的JS代码
        .pipe(babel({
            presets: ['es2015']
        }))
        //js压缩
        // .pipe(rjs())
        .pipe(uglify(
            {
                mangle:{reserved:['require','exports','module']}//压缩代码的保留字
            }
        ))
        .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); }) //增加这一行
        //将转译压缩后的文件输出到dist/static/scripts下（假如没有dist目录则自动生成dist目录）
        .pipe(gulp.dest('dist/scripts'))
});
gulp.task('change', function(){
    gulp.src(files)
        .pipe(connect.reload());
});
gulp.task('start',['connect','js','watch'])